package com.example.usermanagement.service.impl;

import com.example.usermanagement.entity.User;
import com.example.usermanagement.mapper.UserMapper;
import com.example.usermanagement.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public User insert(User user) {
        userMapper.insert(user);
        return user;
    }

    @Override
    @Transactional
    public User update(User user) {
        userMapper.update(user);
        return user;
    }

    @Override
    @Transactional
    public boolean deleteById(Integer id) {
        return userMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional
    public boolean deleteBatch(List<Integer> ids) {
        return userMapper.deleteBatch(ids) > 0;
    }

    @Override
    public User getById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public List<User> getAll() {
        return userMapper.selectAll();
    }

    @Override
    public List<User> getAllOrderBy(String field, String order) {
        return userMapper.selectAllOrderBy(field, order);
    }

    @Override
    public List<User> getByCondition(Map<String, Object> condition) {
        return userMapper.selectByCondition(condition);
    }

    @Override
    public List<User> getByLike(String keyword) {
        return userMapper.selectByLike(keyword);
    }

    @Override
    public PageInfo<User> getPageByLike(String keyword, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.selectByLike(keyword);
        return new PageInfo<>(userList);
    }
} 